package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-log",consumerGroup = "tanhua-log-consumer")
@Slf4j
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;

    @Override
    public void onMessage(String message) {  //{"date":"2021-11-09","type":"0102","userId":101}
        log.info("======插入日志信息======>"+message);
        //1.把json字符串转换为map
        Map map = JSON.parseObject(message, Map.class);
        Integer userId = (Integer) map.get("userId");
        String type = (String) map.get("type");
        String date = (String) map.get("date");
        //2. 把数据封装到log对象中，然后插入log对象
        Log log = new Log();
        log.setUserId(userId.longValue());
        log.setLogTime(date); //用户操作的时间
        log.setType(type);
        log.setCreated(new Date()); //生成日志的时间
        log.setUpdated(new Date());
        logService.save(log);


    }
}
